package javase2.day04.demo01;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Demo04MapSutdent {
    public static void main(String[] args) {
        show01();
        System.out.println("--------------------------------------");
        show02();
        /*
        学生5--->Student{name='汗血宝马', age=100}
        学生2--->Student{name='猪八戒', age=28}
        学生1--->Student{name='孙悟空', age=20}
        学生4--->Student{name='汗血宝马', age=100}
        学生3--->Student{name='唐三藏', age=29}
        --------------------------------------
        Student{name='白虎', age=10}--->西
        Student{name='玄武', age=10}--->bei
        Student{name='麒麟', age=10}--->中
        Student{name='血麒麟', age=10}--->中
        Student{name='朱雀', age=10}--->南
        Student{name='青龙', age=10}--->东
         */
    }

    private static void show01(){
        //创建Map集合用value储存student
        //key继续使用String类，
        //注意：Stirng重写了hashCode和equals方法。所以key唯一若重复添加就会覆盖，而value不受影响可以重复
        Map<String,Student> studentMap=new HashMap<>();
        studentMap.put("学生1",new Student("孙悟空",20));
        studentMap.put("学生2",new Student("猪八戒",28));
        studentMap.put("学生3",new Student("唐三藏",29));
        studentMap.put("学生4",new Student("白龙马",18));
        studentMap.put("学生4",new Student("汗血宝马",100));
        studentMap.put("学生5",new Student("汗血宝马",100));
        //遍历：keyset+增强for循环
        Set<String> stuMapKey = studentMap.keySet();
        for (String str:stuMapKey) {
            System.out.println(str+"--->"+studentMap.get(str));
        }
    }

    private static void show02(){
        //创建Map集合用key储存student
        //value使用Stirng类，
        //注意：Student重写了hashCode和equals方法后才会唯一添加，如重复则覆盖。而value不受影响可以重复
        Map<Student,String> studentMap=new HashMap<>();
        studentMap.put(new Student("麒麟",10),"中");
        studentMap.put(new Student("白虎",10),"西");
        studentMap.put(new Student("朱雀",10),"南");
        studentMap.put(new Student("青龙",10),"东");
        studentMap.put(new Student("玄武",10),"北");
        studentMap.put(new Student("玄武",10),"bei");
        studentMap.put(new Student("血麒麟",10),"中");
        //遍历：entry+Iterator
        Set<Map.Entry<Student, String>> stuEntry = studentMap.entrySet();
        Iterator<Map.Entry<Student, String>> it = stuEntry.iterator();
        while (it.hasNext()){
            Map.Entry<Student, String> entry = it.next();
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"--->"+value);
        }

    }
}
